package com.e1xp.deport_management_system.controller;

import com.e1xp.deport_management_system.entity.OrderProduct;
import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.exception.CustomExceptionType;
import com.e1xp.deport_management_system.service.IOrderService;
import com.e1xp.deport_management_system.service.IProductService;
import com.e1xp.deport_management_system.utils.AddOrderProductWrapper;
import com.e1xp.deport_management_system.utils.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author E1xP@foxmail.com
 * @version 1.0
 * @PACKAGE_NAME com.e1xp.deport_management_system.controller
 * @CLASS_NAME OrderProductController
 * @Description TODO OrderProductController
 * @Date 2021/5/16 下午 11:05
 **/
@Controller
@RequestMapping("/orderProduct")
public class OrderProductController {

    @Resource
    IOrderService orderService;

    @Resource
    IProductService productService;

    /**
     * 用于渲染添加商品到订单页面
     *
     * @param instanceId 实例id
     * @param orderId    订单id
     * @return ModelAndView
     */
    @RequestMapping("/addOrderProductPage")
    public ModelAndView addOrderProductPage(@SessionAttribute long instanceId, long orderId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("orderId", orderId);
        modelAndView.setViewName("addOrderProduct");
        return modelAndView;
    }

    /**
     * 用于渲染订单中商品页面
     *
     * @param instanceId     实例id
     * @param orderProductId 订单详情id
     * @return ModelAndView
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@SessionAttribute long instanceId, long orderProductId) {
        ModelAndView modelAndView = new ModelAndView();
        OrderProduct orderProduct = orderService.getOrderProduct(instanceId, orderProductId);
        if (orderProduct == null) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            String productName = productService.get(instanceId, orderProduct.getProductId()).getName();
            modelAndView.addObject("orderProduct", orderProduct);
            modelAndView.addObject("productName", productName);
            modelAndView.setViewName("changeOrderProduct");
        }
        return modelAndView;
    }

    /**
     * 用于更新订单详细项
     *
     * @param instanceId   实例id
     * @param orderProduct 订单详细项
     * @return AjaxResponse
     * @throws CustomException 当修改后商品数小于0，订单详细项不存在，修改后商品数值小于已拣货，订单不存在，商品不存在，购买数量小于可出售数量,订单状态异常时抛出异常
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResponse update(@SessionAttribute long instanceId, OrderProduct orderProduct) throws CustomException {
        if (orderProduct.getStats() > 1) {
            //修改订单状态限制
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "不允许在拣货完成后修改订单内容");
        }
        orderService.updateOrderProduct(instanceId, orderProduct);
        return AjaxResponse.success();
    }

    /**
     * 用于添加订单详情项
     *
     * @param instanceId             实例id
     * @param addOrderProductWrapper 添加订单详情封装项
     * @return AjaxResponse
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResponse add(@SessionAttribute long instanceId, @RequestBody AddOrderProductWrapper addOrderProductWrapper) throws CustomException {
        orderService.addOrderProduct(instanceId, addOrderProductWrapper);
        return AjaxResponse.success();
    }

    /**
     * 删除订单详情项商品
     *
     * @param instanceId     实例id
     * @param orderProductId 订单详情项
     * @return AjaxResponse
     * @throws CustomException 当找不到该订单详情项，已有拣货时抛出该异常
     */
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResponse delete(@SessionAttribute long instanceId, long orderProductId) throws CustomException {
        orderService.deleteOrderProduct(instanceId, orderProductId);
        return AjaxResponse.success();
    }
}
